D:\a\tools.proto\tools.proto\compiler\src\api\config\core.rs
Line | Count | Source |
1 | | // Copyright (c) 2024, BlockProject 3D |
2 | | // |
3 | | // All rights reserved. |
4 | | // |
5 | | // Redistribution and use in source and binary forms, with or without modification, |
6 | | // are permitted provided that the following conditions are met: |
7 | | // |
8 | | // * Redistributions of source code must retain the above copyright notice, |
9 | | // this list of conditions and the following disclaimer. |
10 | | // * Redistributions in binary form must reproduce the above copyright notice, |
11 | | // this list of conditions and the following disclaimer in the documentation |
12 | | // and/or other materials provided with the distribution. |
13 | | // * Neither the name of BlockProject 3D nor the names of its contributors |
14 | | // may be used to endorse or promote products derived from this software |
15 | | // without specific prior written permission. |
16 | | // |
17 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
18 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
19 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
20 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
21 | | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
22 | | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
23 | | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
24 | | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
25 | | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
26 | | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
27 | | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | |
29 | | use crate::api::config::model::Config; |
30 | | use crate::api::core::generator::{Context, Generator, Params}; |
31 | | use crate::api::core::loader::{Loader, Options}; |
32 | | use crate::api::core::Error; |
33 | | use crate::compiler::util::imports::ImportSolver; |
34 | | use bp3d_debug::trace; |
35 | | use serde::Deserialize; |
36 | | use std::borrow::Cow; |
37 | | use crate::compiler::util::protocols::ProtocolStore; |
38 | | |
39 | 3 | pub fn parse<'a, T: Deserialize<'a>>(data: &'a str) -> Result<Config<'a, T>, toml::de::Error> { |
40 | 3 | toml::from_str(data) |
41 | 3 | } |
42 | | |
43 | 3 | pub fn compile<'a, 'b, T, I: ImportSolver>(config: &'b Config<T>, solver: &'a I) -> Result<ProtocolStore<'a, I, Options<'b>>, Error> { |
44 | 3 | let mut loader = Loader::default(); |
45 | 3 | loader.load_from_folder(config.package.path, &Options::from_package(config.package.name))?0 ; |
46 | 3 | if let Some(deps1 ) = &config.dependency { Branch (46:12): [True: 1, False: 1]
Branch (46:12): [Folded - Ignored]
Branch (46:12): [True: 0, False: 1]
Branch (46:12): [Folded - Ignored]
|
47 | 2 | for dep1 in deps { |
48 | 1 | loader.load_from_file(dep.path, Options::from_package(dep.package).exclude_from_generation())?0 ; |
49 | | } |
50 | 2 | } |
51 | 3 | trace!("Excluded protocols: {:?}", config.package.exclude); |
52 | 3 | if let Some(excludes2 ) = &config.package.exclude { Branch (52:12): [True: 1, False: 1]
Branch (52:12): [Folded - Ignored]
Branch (52:12): [True: 1, False: 0]
Branch (52:12): [Folded - Ignored]
|
53 | 4 | for exclude2 in excludes { |
54 | 2 | loader.exclude(exclude); |
55 | 2 | } |
56 | 1 | } |
57 | 3 | loader.compile(solver) |
58 | 3 | } |
59 | | |
60 | 3 | pub fn generate<'a, G: crate::gen::Generator, T, I: ImportSolver, F: Fn(&T) -> Option<G::Params<'_>>>( |
61 | 3 | generator: &'a Generator<'a, G>, |
62 | 3 | context: &mut Context<'a, I>, |
63 | 3 | config: &Config<T>, |
64 | 3 | generator_params_converter: F, |
65 | 3 | generator_default_params: &G::Params<'_>, |
66 | 3 | ) -> Result<(), Error> { |
67 | 3 | if let Some(options2 ) = &config.options { Branch (67:12): [True: 2, False: 1]
Branch (67:12): [True: 0, False: 0]
Branch (67:12): [Folded - Ignored]
|
68 | 14 | for (protocol, params12 ) in options { |
69 | 12 | let protocol_path: Cow<str> = if config.package.name.is_empty() { Branch (69:46): [True: 12, False: 0]
Branch (69:46): [True: 0, False: 0]
Branch (69:46): [Folded - Ignored]
|
70 | 12 | Cow::Borrowed(protocol) |
71 | | } else { |
72 | 0 | Cow::Owned(format!("{}::{}", config.package.name, protocol)) |
73 | | }; |
74 | 12 | trace!({ path = &*protocol_path }, "Found options for protocol: {}", protocol); |
75 | 12 | let mut p = Params::default(); |
76 | 12 | if let Some(flag1 ) = params.write_messages { Branch (76:20): [True: 1, False: 11]
Branch (76:20): [True: 0, False: 0]
Branch (76:20): [Folded - Ignored]
|
77 | 1 | p.write_messages = flag; |
78 | 11 | } |
79 | 12 | if let Some(flag1 ) = params.read_messages { Branch (79:20): [True: 1, False: 11]
Branch (79:20): [True: 0, False: 0]
Branch (79:20): [Folded - Ignored]
|
80 | 1 | p.read_messages = flag; |
81 | 11 | } |
82 | 12 | if let Some(flag0 ) = params.use_enums { Branch (82:20): [True: 0, False: 12]
Branch (82:20): [True: 0, False: 0]
Branch (82:20): [Folded - Ignored]
|
83 | 0 | p.use_enums = flag; |
84 | 12 | } |
85 | 12 | if let Some(flag0 ) = params.use_structs { Branch (85:20): [True: 0, False: 12]
Branch (85:20): [True: 0, False: 0]
Branch (85:20): [Folded - Ignored]
|
86 | 0 | p.use_structs = flag; |
87 | 12 | } |
88 | 12 | if let Some(flag0 ) = params.use_unions { Branch (88:20): [True: 0, False: 12]
Branch (88:20): [True: 0, False: 0]
Branch (88:20): [Folded - Ignored]
|
89 | 0 | p.use_unions = flag; |
90 | 12 | } |
91 | 12 | if let Some(flag0 ) = params.use_messages { Branch (91:20): [True: 0, False: 12]
Branch (91:20): [True: 0, False: 0]
Branch (91:20): [Folded - Ignored]
|
92 | 0 | p.use_messages = flag; |
93 | 12 | } |
94 | 12 | if let Some(gp11 ) = generator_params_converter(¶ms.inner) { Branch (94:20): [True: 11, False: 1]
Branch (94:20): [True: 0, False: 0]
Branch (94:20): [Folded - Ignored]
|
95 | 11 | context.generate(generator, protocol_path, &p, &gp)?0 ; |
96 | | } else { |
97 | 1 | context.generate(generator, protocol_path, &p, generator_default_params)?0 ; |
98 | | } |
99 | | } |
100 | 1 | } |
101 | 3 | let params = Params::default(); |
102 | 3 | context.generate_all(generator, ¶ms, generator_default_params) |
103 | 3 | } |